package br.com.center.loja.ws;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import br.com.center.loja.data.NamedRepository;
import br.com.center.loja.data.RepositoryFactoryJPA;
import br.com.center.loja.model.Client;
import br.com.center.loja.ws.dto.ClientDTO;
import br.com.center.loja.ws.dto.converter.ClientDTOConverter;

@Path("/client")
public class ClientResource {
	
	
	@GET
	@Produces({MediaType.APPLICATION_JSON})
	public List<ClientDTO> all(){
		List<Client> all = RepositoryFactoryJPA.create(Client.class).all();
		
		return new ClientDTOConverter().to(all);
	}

	@GET
	@Path("/search/name/{query}")
	@Produces({MediaType.APPLICATION_JSON})
	public List<ClientDTO> allByName(@PathParam("query") String name){
		NamedRepository<Client> repository = RepositoryFactoryJPA.createNamed(Client.class);
		List<Client> all = repository.all(name);

		return new ClientDTOConverter().to(all);
	}
	
	@GET
	@Path("/search/code/{query}")
	@Produces({MediaType.APPLICATION_JSON})
	public ClientDTO selectByCode(@PathParam("query") Long code){
		Client client = RepositoryFactoryJPA.create(Client.class).select(code);
		
		return new ClientDTOConverter().to(client);
	}

}
